using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace Weazel.Toad.Application
{
  public class SettingsRegistry
  {
    private const string MAJOR_KEY_NAME = "Toad";

    private bool contains(string[] list, string value)
    {
      foreach (string s in list)
        if (s == value)
          return true;

      return false;
    }

    private RegistryKey getMajorKey()
    {
      RegistryKey key = Registry.CurrentUser;

      if (contains(key.GetSubKeyNames(), MAJOR_KEY_NAME))
        key = key.OpenSubKey(MAJOR_KEY_NAME, true);
      else
        key = key.CreateSubKey(MAJOR_KEY_NAME);

      return key;
    }

    public object GetValue(string label)
    {
      RegistryKey key = getMajorKey();

      object value = null;

      if (contains(key.GetValueNames(), label))
        value = key.GetValue(label);
      else
        value = null;

      key.Close();

      return value;
    }

    public void SetValue(string label, object value)
    {
      RegistryKey key = getMajorKey();
      key.SetValue(label, value);
      key.Close();
    }
  }
}
